﻿@using Shared.Core
@using Shared.Extensions


<div class="fd-field-properties-panel h-100 ppp">
    <Tab>
        <TabItem Text="工具箱">
            <Collapse>
                <CollapseItems>
                    <CollapseItem Text="普通控件" IsCollapsed="false">
                        @if (Widgets != null)
                        {
                            @foreach (var widget in Widgets)
                            {
                                <PaletteWidget WidgetItem="@widget" />
                            }
                        }
                    </CollapseItem>
                     <CollapseItem Text="容器控件">
                        早晚会有..
                     </CollapseItem>
                    <CollapseItem Text="图标控件">
                        早晚会有..
                    </CollapseItem>
                    <CollapseItem Text="交互控件">
                        早晚会有..
                    </CollapseItem>
                </CollapseItems>
            </Collapse>
            @*    <div class="fd-palette-widgets">

            </div> *@
        </TabItem>
        <TabItem Text="菜单管理">
            <Button OnClick="Query">刷新</Button>
            @* <Button OnClick="Expand2">刷新</Button> *@
            <TreeView TItem="TreeComponentData" Items="@_datas" ShowCheckbox="true" ClickToggleNode="true" />
          

        </TabItem>
        <TabItem Text="角色">
            角色
        </TabItem>
    </Tab>
</div>
@code
{
    /// <summary>
    /// 每一个组件都应该有这个属性
    /// </summary>
    [CascadingParameter(Name = "Root")]
    [NotNull]
    public FormDesigner? Root { get; set; }
    /// <summary>
    /// 所有的工具的数据
    /// </summary>
    [Parameter]
    public List<PaletteWidgetDto>? Widgets { get; set; }
    List<TreeViewItem<TreeComponentData>> _datas = new();
    /// <summary>
    /// 容器本身的数据数据
    /// </summary>
    [Parameter]
    [NotNull]
    public ContainerDto? ContainerData { get; set; }
    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        if (Widgets == null)
        {
            Widgets = PaletteWidgetSeeder.GetPaletteWidgets();
        }
        ContainerData.ToTree(_datas);
    }

    //OnClick Expand
    public void Query()
    {
        _datas = new() { };
        ContainerData.ToTree(_datas);
    }
    public void Expand2()
    {
        Expand(_datas);
    }
    public void Expand(List<TreeViewItem<TreeComponentData>> datas)
    {
        foreach (var item in datas)
        {
            if (item.HasChildren)
            {
                item.IsExpand = true;
               
                Expand(item.Items);
            }
        }
    }
    //    public async Task OnClick(TreeEventArgs<TreeComponentData> args)
    //{
    //    var dataItem = ((TreeComponentData)args.Node.DataItem);
    //    switch (dataItem.Type)
    //    {
    //        case ControlType.Comtainer:
    //            ContainerDto? res = null;
    //            ContainerData.FindContainer(x => x.Id == dataItem.Key, ref res);
    //            if (res != null)
    //            {
    //                await Root.SelectContainerAsync(res);
        //            }

    //            break;
    //        case ControlType.Row:
    //            RowDto? resrow = null;
    //            ContainerData.FindRow(x => x.Id == dataItem.Key, ref resrow);
    //            if (resrow != null)
    //            {
    //                await Root.SelectRowAsync(resrow);
    //            }
    //            break;
    //        case ControlType.Component:
    //            ComponentDto? resrom = null;
    //            ContainerData.FindComponent(x => x.Id == dataItem.Key, ref resrom);
    //            if (resrom != null)
    //            {
    //                await Root.SelectComponentAsync(resrom);
    //            }
    //            break;
    //        default:
    //            break;
    //    }
    //}
    //public async Task OnNodeLoadDelayAsync(TreeEventArgs<TreeComponentData> args)
    //{
    //    _datas = new List<TreeViewItem<TreeComponentData>>();
    //    ContainerData.ToTree(_datas);
    //    return;
    //}
}



